package com.yougou.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.lifecycle.ViewModelProvider;

import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputEditText;
import com.yougou.MainActivity;
import com.yougou.R;
import com.yougou.base.BaseActivity;
import com.yougou.ui.register.RegisterActivity;
import com.yougou.utils.ToastUtils;

public class LoginActivity extends BaseActivity {
    
    private ImageView mBtnBack;
    private TextInputEditText mEtUsername;
    private TextInputEditText mEtPassword;
    private MaterialButton mBtnLogin;
    private TextView mTvGoRegister;
    private TextView mTvForgotPassword;
    private ImageView mIvWechatLogin;
    private ImageView mIvQQLogin;
    
    private LoginViewModel mViewModel;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initData();
    }
    
    @Override
    protected void initView() {
        mBtnBack = findViewById(R.id.iv_back);
        mEtUsername = findViewById(R.id.et_username);
        mEtPassword = findViewById(R.id.et_password);
        mBtnLogin = findViewById(R.id.btn_login);
        mTvGoRegister = findViewById(R.id.tv_go_register);
        mTvForgotPassword = findViewById(R.id.tv_forgot_password);
        mIvWechatLogin = findViewById(R.id.iv_wechat_login);
        mIvQQLogin = findViewById(R.id.iv_qq_login);
        
        // 返回按钮
        mBtnBack.setOnClickListener(v -> finish());
        
        // 登录按钮
        mBtnLogin.setOnClickListener(v -> {
            String username = mEtUsername.getText().toString();
            String password = mEtPassword.getText().toString();
            mViewModel.login(username, password);
        });
        
        // 去注册
        mTvGoRegister.setOnClickListener(v -> {
            Intent intent = new Intent(this, RegisterActivity.class);
            startActivity(intent);
        });
        
        // 忘记密码
        mTvForgotPassword.setOnClickListener(v -> {
            Intent intent = new Intent(this, ForgotPasswordActivity.class);
            startActivity(intent);
        });
        
        // 微信登录
        mIvWechatLogin.setOnClickListener(v -> {
            showThirdPartyLoginDialog("微信");
        });
        
        // QQ登录
        mIvQQLogin.setOnClickListener(v -> {
            showThirdPartyLoginDialog("QQ");
        });
    }
    
    @Override
    protected void initData() {
        mViewModel = new ViewModelProvider(this).get(LoginViewModel.class);
        
        // 观察登录结果
        mViewModel.getLoginResult().observe(this, user -> {
            if (user != null) {
                ToastUtils.show(this, "登录成功");
                // 跳转到主页面
                Intent intent = new Intent(this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
            }
        });
        
        // 观察错误
        mViewModel.getError().observe(this, error -> {
            if (error != null) {
                ToastUtils.show(this, error);
            }
        });
    }
    
    private void showThirdPartyLoginDialog(String platform) {
        new android.app.AlertDialog.Builder(this)
                .setTitle(platform + "授权登录")
                .setMessage("是否同意使用" + platform + "账号登录YouGou商城？\n\n这是模拟登录，实际使用需集成" + platform + "SDK")
                .setPositiveButton("同意并登录", (dialog, which) -> {
                    if (platform.equals("微信")) {
                        mViewModel.loginWithWechat();
                    } else {
                        mViewModel.loginWithQQ();
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }
}

